﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;

namespace Storm.TextEditor.Editor
{
	/// <summary>
	/// Used to describe the apperance of text.
	/// </summary>
	[Editor(typeof(TextStyleUIEditor), typeof(UITypeEditor))]
	public class TextStyle
		: ICloneable
	{
		#region Fields

		private string name = null;

		private bool bold      = false;
		private bool italic    = false;
		private bool underline = false;

		private Color foreColor = Color.Black;
		private Color backColor = Color.Transparent;

		#region Events

		/// <summary>
		/// Occurs when the TextStyle has changed.
		/// </summary>
		public event EventHandler Change = null;

		#endregion

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the name of the TextStyle.
		/// </summary>
		[Browsable(true)]
		[Category("Other")]
		[Description("Gets or sets the name of the TextStyle.")]
		public string Name
		{
			get { return name; }
			set
			{
				name = value;
				this.OnChange();
			}
		}

		/// <summary>
		/// Gets or sets whether the TextStyle should use a bold font.
		/// </summary>
		[Browsable(true)]
		[Category("Apperance")]
		[Description("Gets or sets whether the TextStyle should use a bold font.")]
		public bool Bold
		{
			get { return bold; }
			set
			{
				bold = value;
				this.OnChange();
			}
		}

		/// <summary>
		/// Gets or sets whether the TextStyle should use an italic font.
		/// </summary>
		[Browsable(true)]
		[Category("Apperance")]
		[Description("Gets or sets whether the TextStyle should use an italic font.")]
		public bool Italic
		{
			get { return italic; }
			set
			{
				italic = value;
				this.OnChange();
			}
		}

		/// <summary>
		/// Gets or sets whether the TextStyle should use an underlined font.
		/// </summary>
		[Browsable(true)]
		[Category("Apperance")]
		[Description("Gets or sets whether the TextStyle should use an underlined font.")]
		public bool Underline
		{
			get { return underline; }
			set
			{
				underline = value;
				this.OnChange();
			}
		}

		/// <summary>
		/// Gets or sets the foreground color of the TextStyle.
		/// </summary>
		[Browsable(true)]
		[Category("Apperance")]
		[Description("Gets or sets the foreground color of the TextStyle.")]
		public Color ForeColor
		{
			get { return foreColor; }
			set
			{
				foreColor = value;
				this.OnChange();
			}
		}

		/// <summary>
		/// Gets or sets the background color of the TextStyle.
		/// </summary>
		[Browsable(true)]
		[Category("Apperance")]
		[Description("Gets or sets the background color of the TextStyle.")]
		public Color BackColor
		{
			get { return backColor; }
			set
			{
				backColor = value;
				this.OnChange();
			}
		}

		/// <summary>
		/// Gets whether the background color of the TextStyle is transparent.
		/// </summary>
		[Browsable(false)]
		[Category("Other")]
		[Description("Gets whether the background color of the TextStyle is transparent.")]
		public bool Transparent
		{
			get { return (this.BackColor.A == 0); }
		}

		#endregion

		#region Methods

		#region Public

		/// <summary>
		/// Returns the TextStyle as a string.
		/// </summary>
		/// <returns>The TextStyle as a string.</returns>
		public override string ToString()
		{
			if (this.name == null)
				return "TextStyle";

			return this.name;
		}

		#endregion

		#region Protected

		/// <summary>
		/// Raises the Change event.
		/// </summary>
		protected virtual void OnChange()
		{
			if (this.Change != null)
				this.Change(this, EventArgs.Empty);
		}

		#endregion

		#endregion

		/// <summary>
		/// Initializes a new instance of TextStyle.
		/// </summary>
		public TextStyle()
		{
		}

		#region ICloneable Members

		public object Clone()
		{
			TextStyle textStyle = new TextStyle();

			textStyle.BackColor = BackColor;
			textStyle.Bold = Bold;

			textStyle.ForeColor = ForeColor;
			textStyle.Italic = Italic;

			textStyle.Underline = Underline;
			textStyle.name = name;

			return textStyle;
		}

		#endregion
	}
}
